package com.glory.wxmemsicpda.activity

import androidx.databinding.DataBindingUtil
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.adapter.UpdateMaterialQTYAdapter
import com.glory.wxmemsicpda.base.App
import com.glory.wxmemsicpda.base.BaseDialogActivity
import com.glory.wxmemsicpda.bean.EqpInfoBean
import com.glory.wxmemsicpda.bean.MaterialsListBean
import com.glory.wxmemsicpda.bean.net.BaseResponseBean
import com.glory.wxmemsicpda.databinding.ActivityWorkEndTestBinding
import com.glory.wxmemsicpda.utils.NetUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.lzy.okgo.model.Response
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_SimpleDialogEntityCallBack
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import kotlinx.android.synthetic.main.activity_work_end_test.*


class WorkEndTestActivity : BaseDialogActivity() {

    private var viewDataBinding : ActivityWorkEndTestBinding ?= null
    private var mWorkEndEntity: EqpInfoBean ?= null

    private var mDataList: MutableList<MaterialsListBean.MaterialsBean?>? = mutableListOf()
    private var updateMaterialQTYAdapter: UpdateMaterialQTYAdapter = UpdateMaterialQTYAdapter(
        mDataList
    )

    override fun getLayoutResId(): Int {
        return 0
    }

    override fun initView() {
        viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_work_end_test)
        mWorkEndEntity = intent.getSerializableExtra("Entity") as EqpInfoBean?

        setTitle("批次结束作业(测试)")

        mDataList = mWorkEndEntity?.mtM_MAT_LIST as MutableList<MaterialsListBean.MaterialsBean?>? //获取物料批次信息
        //region 测试
        var materialsBean = MaterialsListBean.MaterialsBean()
        materialsBean.qty = "1"
        materialsBean.materiaL_NO = "111"
        materialsBean.materiaL_ID = "111"
        materialsBean.materiaL_NAME = "111"
        materialsBean.materiaL_TYPE = "111"
        mDataList?.add(materialsBean)
        //endregion
        rvMaterialsList.layoutManager = LinearLayoutManager(this)
        rvMaterialsList.adapter = updateMaterialQTYAdapter
        updateMaterialQTYAdapter.setNewData(mDataList)

        freeUI_titleView.setRightListener("结束作业") {
            val map = JsonObject()
            map.addProperty("EQP_ID", mWorkEndEntity?.eqP_ID)
            map.addProperty("LOT_ID", mWorkEndEntity?.loT_ID)
            map.addProperty("INPUT_TYPE", mWorkEndEntity?.inpuT_TYPE)
            map.addProperty("START_TIME", mWorkEndEntity?.starT_TIME)
            map.addProperty("END_TIME", mWorkEndEntity?.enD_TIME)
            map.addProperty("TOTAL_QUANTITY", mWorkEndEntity?.totaL_QUANTITY)
            map.addProperty("TOTAL_GOOD_QUANTITY", mWorkEndEntity?.totaL_GOOD_QUANTITY)
            map.addProperty("TOTAL_FAIL_QUANTITY", mWorkEndEntity?.totaL_FAIL_QUANTITY)
            map.addProperty("TESTER_PASS_QUANTITY", mWorkEndEntity?.testeR_PASS_QUANTITY)
            map.addProperty("TESTER_FAIL_QUANTITY", mWorkEndEntity?.testeR_FAIL_QUANTITY)
            map.add("BIN_LIST", JsonParser().parse(Gson().toJson(mWorkEndEntity?.biN_LIST)).asJsonArray)
            map.add("DEFECT_CODE_LIST", JsonParser().parse(Gson().toJson(mWorkEndEntity?.defecT_CODE_LIST)).asJsonArray)
            map.add(
                "MTM_MAT_LIST",
                JsonParser().parse(Gson().toJson(updateMaterialQTYAdapter.data)).asJsonArray
            )
            NetUtils.requestPosNet(this,
                "PDALotEnd","PDALotEnd",
                map,
                object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<EqpInfoBean?>?>(
                    this
                ) {
                    override fun onSuccess(response: Response<BaseResponseBean<EqpInfoBean?>?>) {
                        super.onSuccess(response)
                        if (response.body()!!.isSuccess(true, supportFragmentManager)) {
//                            showToast("出站成功", true)
                            showMessageDialog(response.body()!!.response.header.resultmessage, supportFragmentManager)
//                            finish()
                        }
                    }

                    override fun onError(response: Response<BaseResponseBean<EqpInfoBean?>?>) {
                        super.onError(response)
                        loadError(response.exception, "PDALotEnd")
                    }
                })
        }
    }
    override fun initData() {
        viewDataBinding?.entity = mWorkEndEntity
    }
    override fun getBaseEntity(): FreeUI_AddViewEntity {
        return FreeUI_AddViewEntity("header", llMain)
    }
    //消息提示框
    private fun showMessageDialog(message: String, fragmentManager: FragmentManager) {
        FreeApi_DialogUtils.getConfirmDialog(
            App.app, "确定", message
        ) {
            finish()
        }.show(fragmentManager, "DIALOG")

    }
}